import httpx
import asyncio
from icecream import ic
from selenium.webdriver.common.keys import Keys
from selenium import webdriver
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.chrome.service import Service as ChromeService
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
import requests

# conf = {
#     'login': 'mrp801@mrptel.mangosip.ru',
#     'password': 'mRp123456',
#     'url': 'https://lk.mango-office.ru/issa/api/400074132/400124808/members/12348127?all',
# }
conf = {
    'login': 'mrp801@mrptel.mangosip.ru',
    'password': 'mRp123456',
    'url1': 'https://lk.mango-office.ru/400074132/400124808/members/index/autoopen/12348127',
    'url': 'https://lk.mango-office.ru/issa/api/400074132/400124808/members/12348127?all',
}

async def client_ssn():
    options_chrome = webdriver.ChromeOptions()
    # options_chrome.add_argument("--disable-notifications")
    options_chrome.add_argument("user-agent=Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko)\
        Chrome/89.0.142.86 Safari/537.36")
    options_chrome.add_argument("--disable-popup-blocking")
    options_chrome.add_argument('--ignore-ssl-errors=yes')
    options_chrome.add_argument('--ignore-certificate-errors')
    # options_chrome.add_argument("auth_session=eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJyIjoiMzQ2czQiLCJpYXQiOjE3MTMxODUxNzUsImlkbiI6IjZtNnZwZzo2bTZ6NmM6Nm04MmEwOjZtdDlsNDo2MDJ4eDppY3M6c2lwOjo6NmEyZW86bnU5bG9mdGxjbDQ1bXRsYjB2czYxb3AyMzcifQ.3eSg35cHPU7EyZSIMI1FEM20jER4GmFINA81ZXuJnNA; issa7=dkhk3q0f4c0sthi4qai3fiti3i; auth_token=eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJyIjoiNHQ4b2UiLCJleHAiOjE3MTM4NTUyOTUsImlkbiI6IjZtNnZwZzo2bTZ6NmM6Nm04MmEwOjZtdDlsNDo2MDJ4eDppY3M6c2lwOjo6NmEyZW86ZGtoazNxMGY0YzBzdGhpNHFhaTNmaXRpM2kifQ.OHIw4HnGJXMK0YcGJ0fQ7jxtWrGs8DQm_BMKOPPCn4A; last-product-account-400074132=400124808%3A1713854997; PHPSESSID=sCXYMMjhcUPkdaMrK8I7h9HYmFVOUaAx; BITRIX_SM_kernel=-crpt-kernel_0; BITRIX_SM_kernel_0=PfnAJdYW_p8Sv4lzzM0IeVemGK_tZ50C-U2-UQcdJBWJU6yzVXiF7YwDEwj8PF5hUHjZnyKRNNEzPd8TKOhBcXtUOHaBeotIsNr2BFyTi2qmh2EN0jINsvL56xcnczRMByqp0VNvAt3AIJ9qNPrn_tZu1OTDNJ9-IupgffdKDNeEj8_RyWXNcW1Y7F0hmkdoFTwML8CNfG77D6y-rbNfIPxvURc04Ut9tjkuG3GJheGDDEmVyhZJJwHxDhKPZiTJrCGsEdl2hpPUTxb_R0be55LNmsBLYkgac8hfWUb2jaArKFzOnDxZnjYFKCnruNRgckQ166fRZVl48nNFPwRi12-OSSKiOB1WEq3YqAG66V4G24lAoEsWnA-veuRTJxRkpLN9ywQhFdebfUno2u8lCIks_RdA9DmHxr-ghoN1MMXtvpnk6onT5MQ2uRi7k63AY897I1rmxJhxqp6VfUmXh8VIOcNS6t06oufFEIsSTDO2bRzgIMBz-Ng4XrA6ZQolDtI1r9lSwDte663jL0LJVZg8EcNypQ8nnDyPn-OtWdcQzozX06f6X7RV6gwNEcwEGB6UAnbt9fNPI-86mG8_-tv5OiWweJwmMyCPJlFNvsfaKoExZuRUUQKuQ8h8-VvxW1aDYBIf2Ewck06wx62Ol7jaFJFtIi1jegqudNWhD0b-2ofv1NfQiOJC6g0BZc8yVttHIXEwk4Ig8_vCbb3DVoEQRNXc6FsBbEHtqNNUv-C3PA; tcp-lk-last-homepage-400074132=%2Fprofile%2F400074132%2F400124808")
    options_chrome.headless = False
    # options_chrome.add_argument('--headless')
    # options_chrome.add_argument('--disable-gpu')
    service = ChromeService(ChromeDriverManager().install())
    with webdriver.Chrome(service=service, options=options_chrome) as driver:
        driver.get(conf['url1'])
        wait = WebDriverWait(driver, 1000)
        username_input = driver.find_element(By.NAME, "login")
        password_input = driver.find_element(By.NAME, "password")
        username_input.send_keys(conf['login'])
        password_input.send_keys(conf['password'])
        # Отправляем форму
        login_button = driver.find_element(By.CLASS_NAME,"prime")
        login_button.click()
        wait = WebDriverWait(driver, 100).until(EC.title_contains("Authorized"))
        wait.get(conf['url1'])
        wait = WebDriverWait(driver, 10).until(EC.title_is("Expected Title of New Page"))
        # Проверяем, что авторизация прошла успешно
        
        
        selenium_cookies = driver.get_cookies()
        driver.quit()
        session = requests.Session()

    # Добавляем cookies из Selenium в сессию requests
        for cookie in selenium_cookies:
            session.cookies.set(cookie['name'], cookie['value'], domain=cookie['domain'])

        # Теперь можно делать запросы с использованием авторизации, установленной в Selenium
        response = session.get(conf['url1'])
        ic(response.text)


# async def client_ssn():
#     cookies = {
#         'auth_session': 'eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJyIjoiMzQ2czQiLCJpYXQiOjE3MTMxODUxNzUsImlkbiI6IjZtNnZwZzo2bTZ6NmM6Nm04MmEwOjZtdDlsNDo2MDJ4eDppY3M6c2lwOjo6NmEyZW86bnU5bG9mdGxjbDQ1bXRsYjB2czYxb3AyMzcifQ.3eSg35cHPU7EyZSIMI1FEM20jER4GmFINA81ZXuJnNA',
#         'issa7': 'dkhk3q0f4c0sthi4qai3fiti3i',
#         'auth_token': 'eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJyIjoiNHQ4b2UiLCJleHAiOjE3MTM4NTUyOTUsImlkbiI6IjZtNnZwZzo2bTZ6NmM6Nm04MmEwOjZtdDlsNDo2MDJ4eDppY3M6c2lwOjo6NmEyZW86ZGtoazNxMGY0YzBzdGhpNHFhaTNmaXRpM2kifQ.OHIw4HnGJXMK0YcGJ0fQ7jxtWrGs8DQm_BMKOPPCn4A',
#         'last-product-account-400074132': '400124808%3A1713854997',
#         'PHPSESSID': 'sCXYMMjhcUPkdaMrK8I7h9HYmFVOUaAx',
#         'BITRIX_SM_kernel': '-crpt-kernel_0',
#         'BITRIX_SM_kernel_0': 'PfnAJdYW_p8Sv4lzzM0IeVemGK_tZ50C-U2-UQcdJBWJU6yzVXiF7YwDEwj8PF5hUHjZnyKRNNEzPd8TKOhBcXtUOHaBeotIsNr2BFyTi2qmh2EN0jINsvL56xcnczRMByqp0VNvAt3AIJ9qNPrn_tZu1OTDNJ9-IupgffdKDNeEj8_RyWXNcW1Y7F0hmkdoFTwML8CNfG77D6y-rbNfIPxvURc04Ut9tjkuG3GJheGDDEmVyhZJJwHxDhKPZiTJrCGsEdl2hpPUTxb_R0be55LNmsBLYkgac8hfWUb2jaArKFzOnDxZnjYFKCnruNRgckQ166fRZVl48nNFPwRi12-OSSKiOB1WEq3YqAG66V4G24lAoEsWnA-veuRTJxRkpLN9ywQhFdebfUno2u8lCIks_RdA9DmHxr-ghoN1MMXtvpnk6onT5MQ2uRi7k63AY897I1rmxJhxqp6VfUmXh8VIOcNS6t06oufFEIsSTDO2bRzgIMBz-Ng4XrA6ZQolDtI1r9lSwDte663jL0LJVZg8EcNypQ8nnDyPn-OtWdcQzozX06f6X7RV6gwNEcwEGB6UAnbt9fNPI-86mG8_-tv5OiWweJwmMyCPJlFNvsfaKoExZuRUUQKuQ8h8-VvxW1aDYBIf2Ewck06wx62Ol7jaFJFtIi1jegqudNWhD0b-2ofv1NfQiOJC6g0BZc8yVttHIXEwk4Ig8_vCbb3DVoEQRNXc6FsBbEHtqNNUv-C3PA',
#         'tcp-lk-last-homepage-400074132': '%2Fprofile%2F400074132%2F400124808',
#     }
#     headers = {
#         'Accept': 'application/json',
#         'Accept-Language': 'ru-RU,ru;q=0.9',
#         'Connection': 'keep-alive',
#         'Cookie': 'auth_session=eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJyIjoiMzQ2czQiLCJpYXQiOjE3MTMxODUxNzUs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L0LJVZg8EcNypQ8nnDyPn-OtWdcQzozX06f6X7RV6gwNEcwEGB6UAnbt9fNPI-86mG8_-tv5OiWweJwmMyCPJlFNvsfaKoExZuRUUQKuQ8h8-VvxW1aDYBIf2Ewck06wx62Ol7jaFJFtIi1jegqudNWhD0b-2ofv1NfQiOJC6g0BZc8yVttHIXEwk4Ig8_vCbb3DVoEQRNXc6FsBbEHtqNNUv-C3PA; tcp-lk-last-homepage-400074132=%2Fprofile%2F400074132%2F400124808',
#         'Authorization': 'Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJyIjoiNHQ4b2UiLCJleHAiOjE3MTM4NTUyOTUsImlkbiI6IjZtNnZwZzo2bTZ6NmM6Nm04MmEwOjZtdDlsNDo2MDJ4eDppY3M6c2lwOjo6NmEyZW86ZGtoazNxMGY0YzBzdGhpNHFhaTNmaXRpM2kifQ.OHIw4HnGJXMK0YcGJ0fQ7jxtWrGs8DQm_BMKOPPCn4A',
#         'Referer': 'https://lk.mango-office.ru/400074132/400124808/members/index',
#         'Sec-Fetch-Dest': 'empty',
#         'Sec-Fetch-Mode': 'cors',
#         'Sec-Fetch-Site': 'same-origin',
#         'Sec-GPC': '1',
#         'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/124.0.0.0 Safari/537.36',
#         'baggage': 'sentry-environment=production,sentry-public_key=6404e4be14274602817316b388031a09,sentry-trace_id=b6f069e78655449b82d5740b41f82d72',
#         'sec-ch-ua': '"Chromium";v="124", "Brave";v="124", "Not-A.Brand";v="99"',
#         'sec-ch-ua-mobile': '?0',
#         'sec-ch-ua-platform': '"Windows"',
#         'sentry-trace': 'b6f069e78655449b82d5740b41f82d72-bdc8fc9baf68caa0-1',
#     }
#     response = requests.get(
#         'https://lk.mango-office.ru/400074132/400124808/members/index',
#         cookies=cookies,
#         headers=headers,
#         auth=(conf['login'], conf['password']),
#     )
#     ic(response.text)

async def main():
    await client_ssn()


if __name__ == '__main__':
    asyncio.run(main())